export function ajaxRequest(url, method, params) {
  return new Promise((resolve, reject) => {
    let data;
    if (method === "POST" || method === "PUT") {
      data = JSON.stringify(params);
    } else {
      data = null;
    }
    $.ajax({
      type: method,
      url,
      data,
      contentType: "application/json;charset=utf-8",
      beforeSend: function(request) {
        if (localStorage.getItem("userInfo")) {
          request.setRequestHeader(
            "Authorization",
            JSON.parse(localStorage.getItem("userInfo")).token
          );
        }
      },

      success: (res) => {
        resolve(res);
      },
      error: (jqXHR, textStatus, errorThrown) => {
        reject(textStatus);
      },
    });
  });
}

// 登录
export const login = (params) => {
  return ajaxRequest("/user/login", "POST", params);
};

// 注册
export const register = (params) => {
  return ajaxRequest("/user/register", "POST", params);
};

// 编辑用户
export const updateUser = (id, params) => {
  return ajaxRequest(`/user/update/${id}`, "POST", params);
};

export const getSomeUser = (params) => {
  return ajaxRequest(`/user/get`, "POST", params);
};
// 新建用户
export const addUser = (params) => {
  return ajaxRequest("/users", "POST", params);
};

// 删除用户
export const deleteUser = (id) => {
  return ajaxRequest(`/user/delete/${id}`, "POST", {});
}

// 书法家
export const getSFJ = (params) => {
  return ajaxRequest(`/sfj/get`, "POST", params);
};
export const addSFJ = (params) => {
  return ajaxRequest(`/sfj`, "POST", params);
};
export const updateSFJ = (id, params) => {
  return ajaxRequest(`/sfj/update/${id}`, "POST", params);
};
export const deleteSFJ = (id, params) => {
  return ajaxRequest(`/sfj/delete/${id}`, "POST", {});
};
// 作品
export const getProperty = (params) => {
  return ajaxRequest(`/property/get`, "POST", params);
};
export const addProperty = (params) => {
  return ajaxRequest(`/property`, "POST", params);
};
export const updateProperty = (id, params) => {
  return ajaxRequest(`/property/update/${id}`, "POST", params);
};
export const deleteProperty = (id, params) => {
  return ajaxRequest(`/property/delete/${id}`, "POST", {});
};
// 材质
export const getMaterial = (params) => {
  return ajaxRequest(`/material/get`, "POST", params);
};
export const addMaterial = (params) => {
  return ajaxRequest(`/material`, "POST", params);
};
export const updateMaterial = (id, params) => {
  return ajaxRequest(`/material/update/${id}`, "POST", params);
};
export const deleteMaterial = (id, params) => {
  return ajaxRequest(`/material/delete/${id}`, "POST", {});
};
// 书法体
export const getStyle = (params) => {
  return ajaxRequest(`/style/get`, "POST", params);
};
export const addStyle = (params) => {
  return ajaxRequest(`/style`, "POST", params);
};
export const updateStyle = (id, params) => {
  return ajaxRequest(`/style/update/${id}`, "POST", params);
};
export const deleteStyle = (id, params) => {
  return ajaxRequest(`/style/delete/${id}`, "POST", {});
};
// 书法体
export const getEvent = (params) => {
  return ajaxRequest(`/event/get`, "POST", params);
};
export const addEvent = (params) => {
  return ajaxRequest(`/event`, "POST", params);
};
export const updateEvent = (id, params) => {
  return ajaxRequest(`/event/update/${id}`, "POST", params);
};
export const deleteEvent = (id, params) => {
  return ajaxRequest(`/event/delete/${id}`, "POST", {});
};
// 趣味知识
export const getArticle = (params) => {
  return ajaxRequest(`/article/get`, "POST", params);
};
export const addArticle = (params) => {
  return ajaxRequest(`/article`, "POST", params);
};
export const updateArticle = (id, params) => {
  return ajaxRequest(`/article/update/${id}`, "POST", params);
};
export const deleteArticle = (id, params) => {
  return ajaxRequest(`/article/delete/${id}`, "POST", {});
};